﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using WTwitter.Model.Framework.TextSplitter;
using System.Diagnostics;
using System.Windows;

namespace WTwitter.View {
	/// <summary>
	/// テキスト部品（テキストをURLやプレーンテキストなどの分解したもの）をViewで表示するためのTextBlockに変換するConverter
	/// </summary>
	[ValueConversion(typeof(List<TextComponent>), typeof(TextBlock))]
	class TextComponentsConverter : IValueConverter {
		private static Style _textStyle = null;
		private static Style textStyle {
			get {
				if (_textStyle == null) {
					_textStyle = App.Current.FindResource("DesignedTextBodyStyle") as System.Windows.Style;
				}
				return _textStyle;
			}
		}
		private static Style _hyperlinkStyle = null;
		private static Style hyperlinkStyle {
			get {
				if (_hyperlinkStyle == null) {
					_hyperlinkStyle = App.Current.FindResource("DesignedHyperlinkStyle") as System.Windows.Style;
				}
				return _hyperlinkStyle;
			}
		}

		#region IValueConverter メンバ

		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			//以下コメントアウト部分は、テキストを選択可能にする場合にTextBlockと入れ替えようとしていたもの
			//FlowDocumentだとマウスクリックを拾わないなどの動作を解決することがあるため、いったんTextBlockにしている
			//var paragraph = new Paragraph();

			//var doc = new FlowDocument(paragraph);
			//doc.PagePadding = new System.Windows.Thickness(0);

			//var viewer = new FlowDocumentScrollViewer();
			//viewer.Document = doc;
			//viewer.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
			//viewer.MaxZoom = 100;
			//viewer.MinZoom = 100;

			var textblock = new TextBlock();
			textblock.Style = textStyle;
			textblock.TextWrapping = System.Windows.TextWrapping.Wrap;

			List<TextComponent> target = value as List<TextComponent>;
			if (target == null) return null;

			foreach (var item in target) {
				switch (item.Type) {
					case TextComponentType.Plain:
						textblock.Inlines.Add(item.Text);
						break;
					case TextComponentType.UserName:
						var user = item as UserName;
						var link = new Hyperlink();
						var userUrl = user.WebUrl;
						link.Click += (sender, e) => this.OpenByBrowser(userUrl);
						link.Inlines.Add(item.Text);
						link.Style = hyperlinkStyle;
						textblock.Inlines.Add(link);
						break;
					case TextComponentType.Url:
						var url = item as Url;
						var urlLink = new Hyperlink();
						var urlUrl = url.Text;
						urlLink.Click += (sender, e) => this.OpenByBrowser(urlUrl);
						urlLink.Inlines.Add("LINK");
						urlLink.ToolTip = urlUrl;
						urlLink.Style = hyperlinkStyle;
						textblock.Inlines.Add(urlLink);
						break;
					case TextComponentType.Tag:
						var tag = item as Tag;
						var tagLink = new Hyperlink();
						var tagUrl = tag.WebUrl;
						tagLink.Click += (sender, e) => this.OpenByBrowser(tagUrl);
						tagLink.Inlines.Add(tag.Text);
						tagLink.Style = hyperlinkStyle;
						textblock.Inlines.Add(tagLink);
						break;
				}
			}

			return textblock;
		}

		private void OpenByBrowser(string url) {
			Process.Start(url);
		}

		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			throw new NotImplementedException();
		}

		#endregion
	}
}
